/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * JtaTransactionManagerConfiguration.java
 * wholebasic
 * 2013年10月25日上午11:54:03
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.config;

import java.beans.PropertyVetoException;
import java.io.IOException;

import javax.sql.DataSource;

import org.jackysoft.bean.define.TransactionNames;
import org.jackysoft.db.DataSourceFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;


@Configuration@MapperScan

// MapperScannerConfigurer
public class IdentifyConfig  extends AbstractSpringConfig {

	@Autowired
	ContextConfig context;
	


	@Bean(name = "idtds")
	public DataSource idtds() {
		String jdbcidt = "jdbc-idt.properties";
		return DataSourceFactory.buildDataSource(jdbcidt);

	}

	@Bean(name = "idtsqlSessionFactory")
	public SqlSessionFactoryBean sqlSessionFactory() throws IOException,
			PropertyVetoException {

		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

		String path  = "classpath*:org/jackysoft/mapper/*Mapper.xml";
		PathMatchingResourcePatternResolver prp = new PathMatchingResourcePatternResolver();
		Resource[] mapperLocations = prp.getResources(path);       
		bean.setDataSource(idtds());
		bean.setTypeAliasesPackage(context.typeAliasesPackage());
		bean.setMapperLocations(mapperLocations);
		bean.setTypeHandlersPackage(context.typeHandlerPackage());		
		return bean;

	}

	@Bean(name = "idtsqlsession")
	public SqlSessionTemplate sqlSession() throws Exception {
		SqlSessionTemplate session = new SqlSessionTemplate(sqlSessionFactory()
				.getObject());
		return session;
	}

	@Bean(name=TransactionNames.IDT_IBATIS_TRANSACTION)
	public DataSourceTransactionManager ibatisTransactionManager(){
		DataSourceTransactionManager bean = new DataSourceTransactionManager();
		bean.setDataSource(idtds());
		return bean;
	}
	
	
}
